package main

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize/v2"
	"path/filepath"
	"strconv"
	"time"
)

// see: https://xuri.me/excelize/zh-hans/
func (f *MainForm) runGenerateTestCase(filePath string, callback func(log string)) {
	callback("开始生成测试用例")
	file := excelize.NewFile()
	streamWriter, err := file.NewStreamWriter("Sheet1")
	if err != nil {
		fmt.Println(err)
		return
	}

	rowId := 1
	_ = file.MergeCell("Sheet1", "A"+strconv.Itoa(rowId), "V"+strconv.Itoa(rowId))
	cell, _ := excelize.CoordinatesToCellName(1, rowId)
	title := "测试用例"
	_ = streamWriter.SetRow(cell, []interface{}{title})

	rowId++
	header := []interface{}{"案例编号", "主题（目录）", "测试阶段", "案例名称", "测试检查要点",
		"案例优先级", "案例性质", "测试类型", "案例状态", "前置条件", "步骤名称", "步骤描述",
		"预期结果", "实际结果", "执行日期", "执行结果", "是否冒烟", "案例设计者", "执行状态",
		"关联测试需求ID", "关联缺陷ID", "备注"}
	cell, _ = excelize.CoordinatesToCellName(1, rowId)
	if err := streamWriter.SetRow(cell, header); err != nil {
		fmt.Println(err)
	}

	tStr := time.Now().Format("2006-01-02")
	for _, menu := range menuConfigs {
		callback("生成菜单[" + menu + "]的测试用例开始")
		rowId++
		listData := []interface{}{rowId - 2, "管理后台测试", "系统测试", menu, menu + "列表数据展示是否正确",
			"高", "正向", "功能测试", "已评审", "登录管理后台", "无", "点击" + menu + "菜单，进入此页面",
			"成功展示列表数据，且数据正确", "成功展示列表数据，且数据正确", tStr, "Passed", "否", "", "已执行",
			"", "", ""}
		cell, _ = excelize.CoordinatesToCellName(1, rowId)
		if err := streamWriter.SetRow(cell, listData); err != nil {
			fmt.Println(err)
		}

		rowId++
		queryData := []interface{}{rowId - 2, "管理后台测试", "系统测试", menu, menu + "能否按照检索条件查询",
			"高", "正向", "功能测试", "已评审", "登录管理后台", "无", "输入需要查询的检索条件",
			"成功返回与输入检索条件匹配的数据", "成功返回与输入检索条件匹配的数据", tStr, "Passed", "否", "", "已执行",
			"", "", ""}
		cell, _ = excelize.CoordinatesToCellName(1, rowId)
		if err := streamWriter.SetRow(cell, queryData); err != nil {
			fmt.Println(err)
		}

		rowId++
		refreshData := []interface{}{rowId - 2, "管理后台测试", "系统测试", menu, menu + "主页数据刷新是否正确",
			"高", "正向", "功能测试", "已评审", "登录管理后台", "无", "点击刷新按钮，重新请求页面数据",
			"成功展示刷新后的列表数据，且数据正确", "成功展示刷新后的列表数据，且数据正确", tStr, "Passed", "否", "", "已执行",
			"", "", ""}
		cell, _ = excelize.CoordinatesToCellName(1, rowId)
		if err := streamWriter.SetRow(cell, refreshData); err != nil {
			fmt.Println(err)
		}

		rowId++
		createData := []interface{}{rowId - 2, "管理后台测试", "系统测试", menu, menu + "能否添加新数据",
			"高", "正向", "功能测试", "已评审", "登录管理后台", "无", "点击新建按钮，输入表单中必填项数据后保存",
			"保存成功," + menu + "增加一条新数据", "保存成功," + menu + "增加一条新数据", tStr, "Passed", "否", "", "已执行",
			"", "", ""}
		cell, _ = excelize.CoordinatesToCellName(1, rowId)
		if err := streamWriter.SetRow(cell, createData); err != nil {
			fmt.Println(err)
		}

		rowId++
		editData := []interface{}{rowId - 2, "管理后台测试", "系统测试", menu, menu + "能否编辑已有数据",
			"高", "正向", "功能测试", "已评审", "登录管理后台", "无", "点击编辑按钮，修改表单中必填项数据后保存",
			"保存成功," + menu + "中编辑的数据显示新修改的内容", "保存成功," + menu + "中编辑的数据显示新修改的内容", tStr, "Passed", "否", "", "已执行",
			"", "", ""}
		cell, _ = excelize.CoordinatesToCellName(1, rowId)
		if err := streamWriter.SetRow(cell, editData); err != nil {
			fmt.Println(err)
		}

		rowId++
		deleteData := []interface{}{rowId - 2, "管理后台测试", "系统测试", menu, menu + "能否删除已有数据",
			"高", "正向", "功能测试", "已评审", "登录管理后台", "无", "点击删除按钮",
			"删除成功," + menu + "中该数据移除", "删除成功," + menu + "中该数据移除", tStr, "Passed", "否", "", "已执行",
			"", "", ""}
		cell, _ = excelize.CoordinatesToCellName(1, rowId)
		if err := streamWriter.SetRow(cell, deleteData); err != nil {
			fmt.Println(err)
		}

		callback("生成菜单[" + menu + "]的测试用例完毕")
	}

	if err := streamWriter.Flush(); err != nil {
		fmt.Println(err)
	}

	filename := filepath.Join(filePath, "测试用例.xlsx")
	if err := file.SaveAs(filename); err != nil {
		fmt.Println(err)
	}
	callback("写入文件（无样式）")

	callback("重新打开文件[" + filename + "]，写入样式")
	file, err = excelize.OpenFile(filename)
	if err != nil {
		fmt.Println(err)
	}

	// 设置宽度
	callback("设置行宽度")
	if err := file.SetColWidth("Sheet1", "A", "C", 15.0); err != nil {
		fmt.Println(err)
	}
	if err := file.SetColWidth("Sheet1", "D", "D", 20.0); err != nil {
		fmt.Println(err)
	}
	if err := file.SetColWidth("Sheet1", "E", "E", 40.0); err != nil {
		fmt.Println(err)
	}
	if err := file.SetColWidth("Sheet1", "F", "K", 15.0); err != nil {
		fmt.Println(err)
	}
	if err := file.SetColWidth("Sheet1", "L", "N", 50.0); err != nil {
		fmt.Println(err)
	}
	if err := file.SetColWidth("Sheet1", "O", "V", 15.0); err != nil {
		fmt.Println(err)
	}

	// 设置边框
	callback("设置边框")
	border := []excelize.Border{
		{Type: "left", Color: "#000000", Style: 1},
		{Type: "right", Color: "#000000", Style: 1},
		{Type: "top", Color: "#000000", Style: 1},
		{Type: "bottom", Color: "#000000", Style: 1},
	}
	bStyle, _ := file.NewStyle(&excelize.Style{Border: border})
	_ = file.SetCellStyle("Sheet1", "A1", "V"+strconv.Itoa(rowId), bStyle)

	// 设置表名样式
	callback("设置标题样式")
	tStyle, _ := file.NewStyle(&excelize.Style{
		Border:    border,
		Fill:      excelize.Fill{Type: "gradient", Color: []string{"#CCFCCC", "#CCFCCC"}},
		Font:      &excelize.Font{Bold: true},
		Alignment: &excelize.Alignment{Horizontal: "center"},
	})
	_ = file.SetCellStyle("Sheet1", "A1", "V1", tStyle)

	// 设置表头样式
	callback("设置表头样式")
	hStyle, _ := file.NewStyle(&excelize.Style{
		Border: border,
		Fill:   excelize.Fill{Type: "gradient", Color: []string{"#FFFF99", "#FFFF99"}},
		Font:   &excelize.Font{Bold: true},
	})
	_ = file.SetCellStyle("Sheet1", "A2", "V2", hStyle)

	callback("写入文件（完整版）")
	if err := file.SaveAs(filename); err != nil {
		fmt.Println(err)
	}
	callback("生成测试用例完毕")
}
